package com.winit.core.server;

import com.winit.core.server.base.CmdOutputHandler;

/**
 * Created by wangjian on 16/8/11.
 */
public class TomcatServer extends Server {

    //tomcat路径
    private String serverPath;

    public TomcatServer(String ip, String userName, Integer port,String serverPath) throws Exception {
        super(ip, userName, port);
        this.serverPath = serverPath;
    }

    /**
     * 停止tomcat
     */
    public void doStopServer(CmdOutputHandler handler) throws Exception{
        this.doExec(handler,this.getStopServerCmd());
    }

    public void doStopServer() throws Exception{
        this.doExec(this.getStopServerCmd());
    }

    private String getStopServerCmd(){
        StringBuffer cmd = new StringBuffer();
        cmd.append("tomcat_pid=`ps -ef | grep -w '").append(this.serverPath)
                .append("' | grep -v 'grep' |grep -v 'tail' | grep -v 'ssh' |grep -v 'bash'|grep -v 'sh'|awk '{print $2}'` && kill -9 ${tomcat_pid}");
        return cmd.toString();
    }

    /**
     * 启动tomcat
     */
    public void doStartServer(CmdOutputHandler handler) throws Exception{
        this.doExec(handler,"source /etc/profile;sh "+this.serverPath+"/bin/startup.sh");
    }

    public void doStartServer() throws Exception{
        this.doExec("source /etc/profile;sh "+this.serverPath+"/bin/startup.sh");
    }

}
